package com.gun.management.module.raffle.service.lotteryrule;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.mybatis.core.util.MyBatisUtils;
import com.gun.management.framework.web.core.util.WebFrameworkUtils;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogRespVO;
import com.gun.management.module.raffle.enums.ErrorCodeConstants;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import com.gun.management.module.raffle.controller.admin.lotteryrule.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotteryrule.LotteryRuleDO;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.util.object.BeanUtils;

import com.gun.management.module.raffle.dal.mysql.lotteryrule.LotteryRuleMapper;

import static com.gun.management.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.gun.management.module.raffle.enums.ErrorCodeConstants.*;

/**
 * 抽奖规则 Service 实现类
 *
 * @author 测试账号
 */
@Service
@Validated
public class LotteryRuleServiceImpl implements LotteryRuleService {

    @Resource
    private LotteryRuleMapper lotteryRuleMapper;

    private final static int LOTTERY_RULE_NOT_EXISTS = 401;

    @Override
    public Long createLotteryRule(LotteryRuleSaveReqVO createReqVO) {
        // 插入
        LotteryRuleDO lotteryRule = BeanUtils.toBean(createReqVO, LotteryRuleDO.class);

        Long userId = WebFrameworkUtils.getLoginUserId();
        lotteryRule.setCreator(String.valueOf(userId));
        lotteryRule.setCreateTime(LocalDateTime.now());

        lotteryRuleMapper.insert(lotteryRule);
        // 返回
        return lotteryRule.getId();
    }

    @Override
    public void updateLotteryRule(LotteryRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateLotteryRuleExists(updateReqVO.getId());
        // 更新
        LotteryRuleDO updateObj = BeanUtils.toBean(updateReqVO, LotteryRuleDO.class);
        Long userId = WebFrameworkUtils.getLoginUserId();
        updateObj.setUpdater(String.valueOf(userId));
        updateObj.setUpdateTime(LocalDateTime.now());

        lotteryRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteLotteryRule(Long id) {
        // 校验存在
        validateLotteryRuleExists(id);
        // 删除
        lotteryRuleMapper.deleteById(id);
    }

    private void validateLotteryRuleExists(Long id) {
        if (lotteryRuleMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.LOTTERY_RULE_NOT_EXISTS);
        }
    }

    @Override
    public LotteryRuleDO getLotteryRule(Long id) {
        return lotteryRuleMapper.selectById(id);
    }

    @Override
    public PageResult<LotteryRuleDO> getLotteryRulePage(LotteryRulePageReqVO pageReqVO) {
        return lotteryRuleMapper.selectPage(pageReqVO);
    }


    /**
     * 根据lotteryId获取对应的奖品列表
     * @param lotteryId
     * @return
     */
    @Override
    public List<LotteryRuleDO> selectLotteryRuleList(Long  lotteryId ) {
        return lotteryRuleMapper.selectLotteryRuleList(lotteryId);
    }



    /**
     * 根据产品id,名称，商品分类 分获取产品及其sku列表
     * @param reqVO
     * @return
     */
    public PageResult<GoodsRespVO> selectGoodsPageList(@Param("reqVO") GoodsPageReqVO reqVO) {

        List<GoodsRespVO> pageResult =  lotteryRuleMapper.selectGoodsPageList(
                reqVO,(reqVO.getPageNo()  - 1) * reqVO.getPageSize() ,reqVO.getPageNo()   * reqVO.getPageSize());

        long total =  lotteryRuleMapper.getTotal(reqVO);



        return   new PageResult<>(pageResult, total);

    }
}